package com.zjmzxfzhl.modules.flowable.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjmzxfzhl.common.core.Result;
import com.zjmzxfzhl.common.core.base.BaseController;
import com.zjmzxfzhl.modules.flowable.entity.FlowableFormOnline;
import com.zjmzxfzhl.modules.flowable.service.FlowableFormOnlineService;
import io.dshubs.commons.security.user.SecurityUser;
import io.dshubs.dto.SysDictDataDTO;
import io.dshubs.dto.SysDictTypeDTO;
import io.dshubs.feign.DictFeignClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static com.alibaba.fastjson.JSON.parseArray;
import static com.alibaba.fastjson.JSON.parseObject;

/**
 * 流程Online
 *
 * @author zhanglinfu
 */
@Api(tags = "流程Online表单")
@RestController
@RequestMapping("/flowable/online")
public class FlowableOnlineController extends BaseController {

    @Autowired
    private FlowableFormOnlineService flowableFormOnlineService;


    @ApiOperation(value = "流程Online表单分页查询")
    @GetMapping(value = "/list")
    public Result list(FlowableFormOnline flowableFormOnline, @RequestParam Integer current, @RequestParam Integer size) {
        IPage<FlowableFormOnline> pageList = flowableFormOnlineService.list(new Page<>(current, size), flowableFormOnline);
        return Result.ok(pageList);
    }

    @ApiOperation(value = "查看详情")
    @GetMapping(value = "/queryById")
    public Result queryById(@RequestParam String id) {
        FlowableFormOnline flowableForm = flowableFormOnlineService.getById(id);
        return Result.ok(flowableForm);
    }

    @ApiOperation(value = "查询全部list")
    @GetMapping(value = "/getAllList")
    public Result list() {
        List<FlowableFormOnline> list = this.flowableFormOnlineService.list();
        return Result.ok(list);
    }

    @ApiOperation(value = "查询Table Schema信息")
    @GetMapping(value = "/getTableSchemaByTableName/{tableName}")
    public Result getTableSchemaByTableName(@PathVariable("tableName") String tableName) {
        if (StringUtils.isBlank(tableName)) {
            return Result.ok();
        }
        List<Map<String, Object>> listMap = this.flowableFormOnlineService.getTableSchemalInfoByTableName(tableName);
        return Result.ok(listMap);
    }

    @ApiOperation(value = "新增Online表单")
    @PostMapping(value = "/save")
    public Result save(@Valid @RequestBody FlowableFormOnline flowableFormOnline) {
        flowableFormOnlineService.save(flowableFormOnline);
        return Result.ok();
    }

    @ApiOperation(value = "修改Online表单")
    @PutMapping(value = "/update")
    public Result update(@Valid @RequestBody FlowableFormOnline flowableFormOnline) {
        flowableFormOnlineService.removeById(flowableFormOnline);
        flowableFormOnlineService.save(flowableFormOnline);
        return Result.ok();
    }

    @ApiOperation(value = "删除Online表单")
    @DeleteMapping(value = "/delete")
    public Result delete(@RequestParam String ids) {
        if (ids == null || ids.trim().length() == 0) {
            return Result.error("ids can't be empty");
        }
        String[] idsArr = ids.split(",");
        if (idsArr.length > 1) {
            flowableFormOnlineService.removeByIds(Arrays.asList(idsArr));
        } else {
            flowableFormOnlineService.removeById(idsArr[0]);
        }
        return Result.ok();
    }
}
